package com.ruoyi.system.interAttr.attr;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 密码游戏列表没有接口
 *
 * 等对面开发人员写好   2024 06 27
 *
 */
@Slf4j
@Service
public class KANGEHAOAttr implements AdapterAttr {


    public static void main(String[] args) {
        SalePartner salePartner = new SalePartner();
        salePartner.setSaleKey("c82ec0881285add376b63a822464a9db");
        salePartner.setOther("2023071081884427");
        long timesteam= System.currentTimeMillis()/1000;
        String cover = salePartner.getOther()+salePartner.getSaleKey()+timesteam+"123456";
        String sign = DigestUtils.md5Hex(cover).toUpperCase();

//        Map<String,Object> params = new HashMap<>();
//        params.put("category_id",47);
//        String response = HttpRequest.post("https://openapi.kangehao.com/categoryAttribute").header("x-kgh-token", "2023071081884427").header("x-kgh-nonce", "123456")
//                .header("x-kgh-signature", sign).header("x-kgh-timestamp", String.valueOf(timesteam))
//                .body(JSONObject.toJSONString(params))
//                .execute().body();
        String url = "https://openapi.kangehao.com/categoryInfo";
        String response = HttpRequest.post(url).header("x-kgh-token", salePartner.getOther()).header("x-kgh-nonce", "123456")
                .header("x-kgh-signature", sign).header("x-kgh-timestamp", String.valueOf(timesteam))
                .execute().body();
        System.out.println(response);
    }

    static Map<String,Object> getSign(SalePartner salePartner){
        Map<String,Object> signMap = new HashMap<>();
        Map<String, Object> params = new HashMap<>();
        params.put("x-kgh-token", salePartner.getOther());
        params.put("x-kgh-nonce", UUID.fastUUID().toString());
        params.put("x-kgh-signature", DigestUtils.md5Hex(salePartner.getOther()+ salePartner.getSaleKey()+ new Date().getTime() / 1000 + "" + UUID.fastUUID().toString()).toUpperCase());
        params.put("x-kgh-timestamp", String.valueOf(new Date().getTime() / 1000));
        String response = HttpUtils.sendPost("https://openapi.kangehao.com/login", JSONObject.toJSONString(params), "application/json");
        String data = JSONObject.parseObject(response).getString("data");
        String nonce = JSONObject.parseObject(data).getString("Nonce");
        String Timestamp = JSONObject.parseObject(data).getString("Timestamp");
        String Signature = JSONObject.parseObject(data).getString("Signature");
        String Token = JSONObject.parseObject(data).getString("Token");
        signMap.put("nonce",nonce);
        signMap.put("timestamp",Timestamp);
        signMap.put("signature",Signature);
        signMap.put("token",Token);
        return signMap;
    }

    @Override
    public List<GamesRes> getGames(SalePartner salePartner) {
        long timesteam= System.currentTimeMillis()/1000;
        String cover = salePartner.getOther()+salePartner.getSaleKey()+timesteam+"123456";
        String sign = DigestUtils.md5Hex(cover).toUpperCase();

        String url = "https://openapi.kangehao.com/categoryInfo";
        String result = HttpRequest.post(url).header("x-kgh-token", salePartner.getOther()).header("x-kgh-nonce", "123456")
                .header("x-kgh-signature", sign).header("x-kgh-timestamp", String.valueOf(timesteam))
                .execute().body();
        JSONObject resJson = JSONObject.parseObject(result);
        List<JSONObject> jsonObjects = JSONObject.parseArray(resJson.getString("data"), JSONObject.class);
        List<GamesRes> gamesResList = new ArrayList<>();

        for (JSONObject jsonObject : jsonObjects) {
            GamesRes gamesRes = new GamesRes();
            String id = jsonObject.getString("id");
            String title = jsonObject.getString("game_name");
            gamesRes.setId(id);
            gamesRes.setGameName(title);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    @Override
    public List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        String url = "https://openapi.kangehao.com/areaInfo";
        Map<String,Object> params = new HashMap<>();
        params.put("category_id",Integer.parseInt(gameId));
        String nonce = UUID.fastUUID().toString();
        String timestamp = String.valueOf(new Date().getTime() / 1000);
        String cover = salePartner.getOther()+salePartner.getSaleKey()+timestamp+nonce;
        String sign = DigestUtils.md5Hex(cover).toUpperCase();
        String response = HttpRequest.post(url).header("x-kgh-token", salePartner.getOther()).header("x-kgh-nonce", nonce)
                .header("x-kgh-signature", sign).header("x-kgh-timestamp", timestamp)
                .body(JSONObject.toJSONString(params))
                .execute().body();
        JSONObject jsonObject = JSONObject.parseObject(response);
        List<JSONObject> data = JSONObject.parseArray(jsonObject.getString("data"), JSONObject.class);

        List<InterServerRes> interServerRes = new ArrayList<>();
        for (JSONObject datum : data) {
            InterServerRes interServer = new InterServerRes();
            interServer.setId(datum.getString("id"));
            interServer.setName(datum.getString("service_name"));
            String servers = datum.getString("children");
            if(StringUtils.isNotEmpty(servers)) {
                List<InterServerRes> interServerRes1 = new ArrayList<>();
                List<JSONObject> jsonObjects = JSONObject.parseArray(servers, JSONObject.class);
                for (JSONObject object : jsonObjects) {
                    InterServerRes interServer1 = new InterServerRes();
                    String id1 = object.getString("id");
                    String title1 = object.getString("service_name");
                    interServer1.setId(id1);
                    interServer1.setName(title1);
                    interServerRes1.add(interServer1);
                }
                interServer.setInterServerRes(interServerRes1);
            }
            interServerRes.add(interServer);
        }
        return interServerRes;
    }

    @Override
    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        String url = "https://openapi.kangehao.com/categoryAttribute";
        Map<String,Object> params = new HashMap<>();
        params.put("category_id",Integer.parseInt(gameId));
        String nonce = UUID.fastUUID().toString();
        String timestamp = String.valueOf(new Date().getTime() / 1000);
        String cover = salePartner.getOther()+salePartner.getSaleKey()+timestamp+nonce;
        String sign = DigestUtils.md5Hex(cover).toUpperCase();
        String response = HttpRequest.post(url).header("x-kgh-token", salePartner.getOther()).header("x-kgh-nonce", nonce)
                .header("x-kgh-signature", sign).header("x-kgh-timestamp", timestamp)
                .body(JSONObject.toJSONString(params))
                .execute().body();
        List<InterAttrRes> interAttrResList = new ArrayList<>();
        JSONObject jsonObject = JSONObject.parseObject(response);
        List<JSONObject> data = JSONObject.parseArray(jsonObject.getString("data"), JSONObject.class);
        List<JSONObject> collect = new ArrayList<>();
        if (Objects.equals("1",type)){
            collect = data.stream().filter(d -> d.getString("value_type").equals("5")).collect(Collectors.toList());
        }else if  (Objects.equals("2",type)){
            collect = data.stream().filter(d -> d.getString("value_type").equals("6")).collect(Collectors.toList());
        }else if (Objects.equals("3",type)){
            collect = data.stream().filter(d -> "1,3,4".contains(d.getString("value_type"))).collect(Collectors.toList());
        }
        for (JSONObject object : collect) {
            InterAttrRes interAttrRes = new InterAttrRes();
            interAttrRes.setId(object.getString("attribute_id"));
            interAttrRes.setName(object.getString("title"));
            List<InterAttrRes> interAttrResList1 = new ArrayList<>();
            String string = object.getString("option");
            if(StringUtils.isNotEmpty(string)) {
                JSONArray jsonArray = JSONArray.parseArray(string);
                for (Object o : jsonArray) {
                    InterAttrRes interAttrRes1 = new InterAttrRes();
                    interAttrRes1.setId(JSONObject.parseObject(o.toString()).getString("option_value"));
                    interAttrRes1.setName(JSONObject.parseObject(o.toString()).getString("option_name"));
                    interAttrResList1.add(interAttrRes1);
                    interAttrRes.setInterServerRes(interAttrResList1);
                }
            }
            interAttrResList.add(interAttrRes);
        }
        return interAttrResList;
    }
}